<?php
declare (strict_types = 1);

namespace app\index\middleware;
use app\common\model\system\Statement as StatementModel;

class Statement
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        if(!$request->isAjax()){
            $model = StatementModel::where([
                ['session', '=', app()->session->getId()],
                ['create_time', '>', strtotime(date('Y-m-d 0:0:0',time()))]
            ])->findOrEmpty();

            $model->save([
                'session' => app()->session->getId(),
                'browser' => getBrowser(),
                'ip' => ip2long($request->ip()),
                'visited' => (int) $model->visited + 1,
                'user_id' => app()->user->isGuest() ? 0 : app()->user->getId(),
            ]);
        }


        return $next($request);
    }
}
